﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using cde.SystemCommon;
using Cit.CDE.Net.Module;
using Cit.CDE.Net.Module.Commons;
using Module;
using UI.UserControl;
using System.Web.UI.HtmlControls;
using System.Text;

/// <summary>
/// Copyright (C) jinggubang
/// FileName:        UserList.aspx.cs       
/// Author :         baishq
/// CDT:             2011-12-13
/// Version:         1.0.0.0                
/// Depiction:       用户管理查询
/// </summary>
public partial class main_admin_UserList : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    private String EM;
    /// <summary>
    /// 用户管理Entity
    /// </summary>
    private CDEEntity m_entityUserManage;
    public string chkID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.strHc = "0";
        //设置母版页控件显示
        ((Literal)Master.FindControl("lblpath")).Text = MenuPath;

        EM = Request.QueryString[WEBConstants.EM_QUERY_STRING];

        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        Master.sc = this.CreatSc(em);

        //设置母版页控件事件
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ItemDataBound += new DataGridItemEventHandler(this.itemDataBound);

     

        // 更改提示框的数据
        ((HiddenField)Master.FindControl("hidDelSelect")).Value = "请选择要禁用的数据。";
        ((HiddenField)Master.FindControl("hidDelRequest")).Value = "确实要禁用吗？";
    }

    /// <summary>
    /// 重置密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdReSetPWD_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        int id = Convert.ToInt32(((HtmlInputCheckBox)((DataGridItem)btn.Parent.Parent.Parent).Cells[0].FindControl("chkMeisai")).Value);
        string strErr = string.Empty;
        string strMsg = string.Empty;
        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM]; // (List<CDEEntity>)Session["listEntity"];
        List<CDEEntity> listEntity = new List<CDEEntity>();
        // 默认密码
        string defaultPSW = em.getAttribute("PSW").Default_value;

        CDESearchCriteria scCheck = new CDESearchCriteria(em);
        scCheck.addCondition("ID", id);
        scCheck.PageSize = 0;
        List<CDEEntity> listCheck = CDEService.LoadEntities(em, scCheck);

        m_entityUserManage = listCheck[0];

        m_entityUserManage.set("PSW", defaultPSW);
        m_entityUserManage.set("UPDATE_USER", op.SysID);
        m_entityUserManage.set("UPDATE_DATE", Now);
        m_entityUserManage.Operation = CDEDataOperation.UPDATE;
        listEntity.Add(m_entityUserManage);
        CDEService.UpdateEntities(em, listEntity);
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = S_listEntity;
        Master.GetFirstNum();
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();
        Session[WEBConstants.LIST_SESSION_ID + EM] = S_listEntity;  //Session["listEntity"] = S_listEntity;

        for (int index = 0; index < listEntity.Count; index++)
        {
            if (listEntity[index].Result == CDEDataOperationResult.ERROR)
            {
                strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
            }
        }
        if (strErr != string.Empty)
        {
            strErr = strErr.Substring(0, strErr.Length - 1);
            strMsg = "重置密码失败!";
            strMsg = strMsg.Replace("{0}", strErr);
            showMessageErrAndOk(strMsg);
        }
        else
        {
            //系统日志
            //SysLog.AddSysLog(listEntity, this.GetLoginOprator());
            showMessageErrAndOk("密码重置为：" + (defaultPSW.Trim() == string.Empty ? "空" : defaultPSW));
        }
        //重置页面条数
        //Master.refreshPage();
        //Master.refreshResult();
    }
    /// <summary>
    /// 操作事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void itemDataBound(object sender, DataGridItemEventArgs e)
    {
        // 操作列显示控制
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns[((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns.Count - 1].Visible = true;
    }

    /// <summary>
    /// 删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdDel_Click(object sender, EventArgs e)
    {

        string strErr = string.Empty;
        string strMsg = string.Empty;
        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
        // 从Session里取ListEntity
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];
        List<CDEEntity> listEntity = new List<CDEEntity>();

        // 置删除标志
        foreach (int index in Master.listIndex)
        {
            S_listEntity[index].set("DEL_FLG", 1);
            S_listEntity[index].set("UPDATE_USER", op.SysID);
            S_listEntity[index].set("UPDATE_DATE", Now);
            S_listEntity[index].Operation = CDEDataOperation.UPDATE;
            listEntity.Add(S_listEntity[index]);
        }

        // 更新EM
        CDEService.UpdateEntities(em, listEntity);

        // ListEntity 绑定到 SimpleResultDataGrid
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = S_listEntity;

        // 置Session之ListEntity
        Session[WEBConstants.LIST_SESSION_ID + EM] = S_listEntity;

        Master.GetFirstNum();

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();

        // 检查
        for (int index = 0; index < listEntity.Count; index++)
        {
            if (listEntity[index].Result == CDEDataOperationResult.ERROR)
            {
                strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
            }
        }
        if (strErr != string.Empty)
        {
            strErr = strErr.Substring(0, strErr.Length - 1);
            strMsg = Resources.ResourceErrMsg.DELETE_ERR;
            strMsg = strMsg.Replace("{0}", strErr);
            showMessageErrAndOk(strMsg);
        }
        else
        {
            //系统日志
            //SysLog.AddSysLog(listEntity, this.GetLoginOprator());

            //showMessageErrAndOk(Resources.ResourceAdmin.MSG_ROLE_DEL_RESULT_OK);
        }
    }
}
